# -*- coding:utf-8 -*-
import time

from selenium.webdriver.common.by import By

from PageClass.common.boeCommon import BoeCommon
from PageClass import BasIndexPage
from Util import logger


class Class_NewGeneralLedgerBoe(BasIndexPage, BoeCommon):
    def __init__(self, driver):
        super().__init__(driver)

    # 打开流水管理页面
    def goToBoePage(self, url):
        self.driver.get(url)
        logger.info("打开流水管理页面")

    # 主表区-记账类型
    def click_operationTypeId(self, operationTypeId):
        # operationTypeId：记账类型编号
        # 点击记账类型
        _element = (By.XPATH, f'//input[@id="boeHeader.0.operationTypeId"]')
        # self.send_text(operationTypeId, *_element)
        self.click(*_element)
        # 点击选中
        _select = (By.XPATH, f'//tbody/tr/td/div[text()="{operationTypeId}"]')
        self.click(*_select)

    # 凭证分录-'+'按钮
    def clickAddByHandle(self):
        _button = (By.XPATH,
                   '//button[@class="el-button el-tooltip button item el-button--default el-button--mini is-circle"]/i[@class="el-icon-plus"]')
        self.click(*_button)

    # 凭证分录-会计科目
    def click_accountName(self, row_num: int, account_code):
        # row_num：行
        # account_code：会计科目编码
        # 避免偶现缺陷，给三次机会
        row_num_tmp = row_num
        for _ in range(3):
            row_num = row_num_tmp
            try:
                row_num = str(int(row_num) - 1)
                _select = (By.XPATH, f'//input[@id="voucher.{row_num}.accountId"]')
                self.lovSelectTable(account_code, *_select)
            except Exception as error:
                try:  # 尝试选择第一行
                    row_1 = (By.XPATH, f"//div[@id='voucher.{row_num}.accountId.table']/div[1]/div/div[3]/div/div[2]/table[1]/tbody/tr/td/div/div[text()='1']")
                    self.click(*row_1)
                except:
                    pass
                else:
                    return
                if _ == 2:
                    logger.error(f"总账单-凭证分录-会计科目，选择第{row_num_tmp}行会计科目编码异常")
                    raise error
            else:
                logger.info(f"成功选择第{row_num_tmp}行会计科目编码")
                return True

    # 凭证分录-借方金额
    def click_debitAmount(self, row_num: int, text):
        # row_num：行
        # text：金额
        row_num = str(int(row_num) - 1)
        # 点击第row_num行的会计科目字段
        _element = (By.XPATH, f'//div[@id="voucher.{row_num}.debitAmount"]')
        self.send_text(text, *_element)

    # 凭证分录-贷方金额
    def click_creditAmount(self, row_num: int, text):
        # row_num：行
        # text：金额
        row_num = str(int(row_num) - 1)
        # 点击第row_num行的会计科目字段
        _element = (By.XPATH, f'//div[@id="voucher.{row_num}.creditAmount"]')
        self.send_text(text, *_element)

    # 主表区-总账记账日期
    def select_gldate(self):
        _element = (By.ID, 'boeHeader.0.glDate')
        self.click(*_element)
        time.sleep(1)
        self.select_date(time.strftime('%Y-%m-%d').split('-')[0], time.strftime('%Y-%m-%d').split('-')[1],
                         time.strftime('%Y-%m-%d').split('-')[2])  # 取当天

    # 单据保存
    _boeSaveButton = (By.ID, 'save')

    def click_boeSaveButton(self):
        self.click(*self._boeSaveButton)
        logger.info("点击保存单据")

    # 单据提交
    _boeSubmitButton = (By.XPATH,
                        '//button[@id="submit" and @class="el-button button el-button--default el-button--small is-round submit success"]')

    def click_boeSubmitButton(self):
        self.click(*self._boeSubmitButton)
        logger.info("点击提交单据")
        message = None
        try:
            message = self.getBoeSuccessMessage()
        except Exception as e:
            self.click_ignoreButton()
            message = self.getBoeSuccessMessage()
        finally:
            return message

    BoeSuccessMsg = (By.XPATH, '//*[@class="alert-info"]')

    def getBoeSuccessMessage(self):
        # 提单成功结果页面提示语
        return self.get_elementText(*self.BoeSuccessMsg)
